const tagsView = {
  state: {
    visitedViews: []
  },
  mutations: {
    ADD_VISITED_VIEWS: (state, view) => {
      const findView = state.visitedViews.find(v => v.path === view.path)
      if (findView) {
        findView.fullPath = view.fullPath
        return
      }
      let title = view.meta.title
      if (view.params && view.params.operType) {
        let operName = ''
        switch (view.params.operType) {
          case 'create':
            operName = '新增'
            break
          case 'edit':
            operName = '编辑'
            break
          case 'look':
            operName = '查看'
            break
          case 'detali':
            operName = '详情'
            break
        }
        if (view.meta.titles && view.meta.titles[view.params.operType]) {
          title = view.meta.titles[view.params.operType]
        } else {
          title = operName + view.meta.title
        }
      }
      if (JSON.parse(sessionStorage.getItem('visitedViews'))) {
        if (JSON.parse(sessionStorage.getItem('visitedViews')).length > 0) {
          state.visitedViews = JSON.parse(sessionStorage.getItem('visitedViews'))
          for (var i = 0; i < state.visitedViews.length; i++) {
            if (view.fullPath === state.visitedViews[i].fullPath) {
              state.visitedViews.splice(i, 1)
            }
          }
        } else {
          state.visitedViews = state.visitedViews
        }
      } else {
        state.visitedViews = state.visitedViews
      }
      state.visitedViews.push({
        name: view.name,
        path: view.path,
        fullPath: view.fullPath,
        title: title || 'no-name'
      })
      sessionStorage.setItem('visitedViews', JSON.stringify(state.visitedViews))
    },
    DEL_VISITED_VIEWS: (state, view) => {
      for (const [i, v] of state.visitedViews.entries()) {
        if (v.path === view.path) {
          state.visitedViews.splice(i, 1)
          break
        }
      }
      sessionStorage.setItem('visitedViews', JSON.stringify(state.visitedViews))
    },
    DEL_OTHERS_VIEWS: (state, view) => {
      for (const [i, v] of state.visitedViews.entries()) {
        if (v.path === view.path) {
          state.visitedViews = state.visitedViews.slice(i, i + 1)
          break
        }
      }
      sessionStorage.setItem('visitedViews', JSON.stringify(state.visitedViews))
    },
    DEL_ALL_VIEWS: state => {
      state.visitedViews = []
      sessionStorage.setItem('visitedViews', JSON.stringify(state.visitedViews))
    }
  },
  actions: {
    addVisitedViews ({ commit }, view) {
      commit('ADD_VISITED_VIEWS', view)
    },
    delVisitedViews ({ commit, state }, view) {
      return new Promise(resolve => {
        commit('DEL_VISITED_VIEWS', view)
        resolve([...state.visitedViews])
      })
    },
    delOthersViews ({ commit, state }, view) {
      return new Promise(resolve => {
        commit('DEL_OTHERS_VIEWS', view)
        resolve([...state.visitedViews])
      })
    },
    delAllViews ({ commit, state }) {
      return new Promise(resolve => {
        commit('DEL_ALL_VIEWS')
        resolve([...state.visitedViews])
      })
    }
  }
}

export default tagsView
